﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCExtension;
using MVCExtension.Pager;
using FTchinaMVC.Models;

namespace FTchinaMVC.Controllers
{
    public class Default2Controller : BaseController
    {
        private FTchinaMVCContext db = new FTchinaMVCContext();

        //
        // GET: /Default2/

        public ActionResult Index(int? id)
        {

            string key = "BookModelsItemsCount";
            if (id == null) { id = 1; }
            int ItemsCount = 0;
            #region 缓存取总数
            if (MVCExtension.FrameWork.CacheHelper.IsExits(key))
            {
                ItemsCount = (int)MVCExtension.FrameWork.CacheHelper.GetObjectCache(key);
            }
            else
            {
                ItemsCount = db.BookModels.Count();
                MVCExtension.FrameWork.CacheHelper.AddObjectCache(key, ItemsCount);
            }
            #endregion
            ViewBag.ItemsCount = ItemsCount;
            ViewBag.PageSize = 15;
            ViewBag.PageIndex = id;
            int skip = ((int)id - 1) * 15;


            var bookmodels = db.BookModels.OrderByDescending(m => m.BookID)
                                            .Skip(skip).Take((int)ViewBag.PageSize);
            return View(bookmodels.ToList());






        }


        /// <summary>
        /// 导出到Excel
        /// </summary>
        public void Excel()
        {
            MVCExtension.Office.ExcelHelper.Export2Excel<Book>(db.BookModels.ToList<Book>());
        }

        //
        // GET: /Default2/Details/5
       
        public ActionResult Details(int id = 0)
        {
            Book book = db.BookModels.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        //
        // GET: /Default2/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Default2/Create

        [HttpPost]
        public ActionResult Create(Book book)
        {
            if (ModelState.IsValid)
            {
                db.BookModels.Add(book);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(book);
        }

        //
        // GET: /Default2/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Book book = db.BookModels.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        //
        // POST: /Default2/Edit/5

        [HttpPost]
        public ActionResult Edit(Book book)
        {
            if (ModelState.IsValid)
            {
                db.Entry(book).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(book);
        }

        //
        // GET: /Default2/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Book book = db.BookModels.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        //
        // POST: /Default2/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Book book = db.BookModels.Find(id);
            db.BookModels.Remove(book);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}